package com.engine4cloud.tdf.supervision.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.engine4cloud.tdf.common.core.exception.ValidateCodeException;
import com.engine4cloud.tdf.common.data.web.query.Pages;
import com.engine4cloud.tdf.common.data.web.query.Queries;
import com.engine4cloud.tdf.common.data.web.response.PageInfo;
import com.engine4cloud.tdf.common.data.web.response.Params;
import com.engine4cloud.tdf.supervision.convert.DcpgSurveyConvert;
import com.engine4cloud.tdf.supervision.dto.create.DcpgSurveyCreateDTO;
import com.engine4cloud.tdf.supervision.dto.query.DcpgEvaluateQueryDTO;
import com.engine4cloud.tdf.supervision.dto.result.DcpgInvestigationResultDTO;
import com.engine4cloud.tdf.supervision.dto.result.DcpgSurveyResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.DcpgSurveyUpdateDTO;
import com.engine4cloud.tdf.supervision.entity.DcpgSurveyEntity;
import com.engine4cloud.tdf.supervision.mapper.DcpgSurveyMapper;
import com.engine4cloud.tdf.supervision.service.DcpgInvestigationService;
import com.engine4cloud.tdf.supervision.service.DcpgSurveyService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * 调查评估-调查
 *
 * @author wenjie
 * @date 2024-02-29 11:07:43
 */
@Service
public class DcpgSurveyServiceImpl extends ServiceImpl<DcpgSurveyMapper, DcpgSurveyEntity> implements DcpgSurveyService {

    @Resource
    private DcpgInvestigationService investigationService;

    @Override
    public PageInfo<DcpgSurveyResultDTO> pageList(DcpgEvaluateQueryDTO dto) {
        IPage<DcpgSurveyResultDTO> page = this.baseMapper.pageList(Pages.page(dto), dto);
        return Pages.convert(page);
    }

    @Override
    public DcpgSurveyResultDTO getById(Long id) {
        return DcpgSurveyConvert.INSTANCE.convert(super.getById(id));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean create(DcpgSurveyCreateDTO dto) {
        DcpgInvestigationResultDTO inv = Optional.ofNullable(investigationService.getByCode(dto.getDcpgbh()))
                .orElseThrow(() -> new ValidateCodeException("委托数据不存在"));

        return save(DcpgSurveyConvert.INSTANCE.create(dto, inv.getTenantId()));
    }

    @Override
    public Boolean update(DcpgSurveyUpdateDTO dto) {
        DcpgSurveyEntity survey = Optional.ofNullable(super.getById(dto.getId()))
                .orElseThrow(() -> new ValidateCodeException("调查评估数据不存在"));

        return updateById(DcpgSurveyConvert.INSTANCE.update(dto));
    }

    @Override
    public List<DcpgSurveyResultDTO> findList(DcpgEvaluateQueryDTO dto) {
        return list(wrapper(dto)).stream().map(DcpgSurveyConvert.INSTANCE::convert).toList();
    }

    private LambdaQueryWrapper<DcpgSurveyEntity> wrapper(DcpgEvaluateQueryDTO query) {
        LambdaQueryWrapper<DcpgSurveyEntity> wrapper = Queries.lambda();
        Queries.accept(query.getId(), v -> wrapper.eq(DcpgSurveyEntity::getId, v));
        Queries.accept(query.getDcpgbh(), v -> wrapper.eq(DcpgSurveyEntity::getDcpgbh, v));
        return wrapper;
    }
}